#include <cstdio>
#include <fcntl.h>
#include <unistd.h>
#include <iservice_registry.h>
#include "usart_proxy.h"

    using namespace OHOS;
using namespace OHOS::Usart_Standard;

int main() {
    auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
    sptr<IRemoteObject> object = samgr->GetSystemAbility(9901);
    if (object == nullptr)
    {
        fprintf(stderr, "Get UsartService failed\n");
        return 0;
    }
    auto mServiceProxy = iface_cast<IUsartAbility>(object);
    if (mServiceProxy == nullptr)
    {
        fprintf(stderr, "Cast proxy failed\n");
        return 0;
    }
    //真正的端口是 “dev/ttyAMA0”
    const std::string name = "/dev/ttyAMA0";
    int ret1 = mServiceProxy->open(name);
    if (ret1 != 0) {
        fprintf(stderr, "open uart failed\n");
        return 0;
    }

    bool ret2 =  mServiceProxy->is_open(name);
    if (!ret2) {
        fprintf(stderr, "is_open uart failed\n");
        return 0;
    }

    std::string s = "test!";
    mServiceProxy->write(name, s);

    fprintf(stderr, "start readline!\n");
    std::string ret4 = mServiceProxy->readline(name);

    
    return 0;
}